<template>
  <div class="permisssion-container">
    <div class="app-container">
      <el-card>
        <div style="text-align:right; margin-bottom:20px;">
          <el-button type="primary" size="small" @click="add(1,'0')">添加权限</el-button>
        </div>
      </el-card>
      <!-- 表格 -->
      <!-- 给表格组件绑定一个row-key="id" row-key的值为数据中的唯一字段key 一般是id
      可以让表格组件支持显示树形结构的数据 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="text" @click="add(2,row.id)">添加</el-button>
            <el-button type="text" @click="edit(row.id)">编辑</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Permisssiondialog ref="permissionRef" :dialogvisible="dialogvisible" :type="type" :pid="pid" @close="close" @updataList="fetchPermissionList" />
  </div>
</template>

<script>
import { getpermission, delPermission } from '@/api'
import findSon from '@/utils/findSon'
import Permisssiondialog from './components/permission-dialog.vue'
export default {
  components: {
    Permisssiondialog
  },
  data() {
    return {
      list: [],
      dialogvisible: false,
      type: 0,
      pid: ''
    }
  },
  created() {
    this.fetchPermissionList()
  },
  methods: {
    async fetchPermissionList() {
      const { data } = await getpermission()
      this.list = findSon(data, '0')
    },
    // 打开弹窗
    add(type, pid) {
      this.type = type
      this.pid = pid
      this.dialogvisible = true
    },
    // 关闭弹窗
    close() {
      this.dialogvisible = false
    },
    // 编辑
    async edit(id) {
      await this.$refs.permissionRef.fetchDetail(id)
      this.dialogvisible = true
    },
    // 删除
    del(id) {
      // 二次确认
      this.$confirm('确定删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delPermission(id)
        // 3. 提示用户
        this.$message.success('删除权限点成功')
        // 4. 刷新列表
        this.fetchPermissionList()
      })
    }
  }

}
</script>

<style>

</style>
